// Unit test for Village Card

#include "dominion.h"  
#include "dominion_helpers.h"  
#include "rngs.h"  
#include <stdio.h>  
#include <math.h>  
#include <stdlib.h>  
#include <assert.h>

#define MAX_CARD 26

int testVillageCard(int numPlayers, int * k, struct gameState *g){
    int i, j, player, topCard, card, retVal, cost, handCountBefore, handCountAfter, actionsBefore, actionsAfter, playedBefore, playedAfter;
    // choose random player
    player = floor(Random()*numPlayers);
    g->deckCount[player] = 5;
    // if hand is empty, draw 5 from deck
    if(g->handCount[player] == 0){
      for(i = 0 ; i < 5; i++){
        topCard = g->deckCount[player]-1;
        g->hand[player][i] = g->deck[player][topCard];
        g->deckCount[player]--;
        g->handCount[player]++;
      }
    }
    g->whoseTurn = player;
    handCountBefore = g->handCount[player];
    actionsBefore = g->numActions;
    playedBefore = g->playedCardCount;
    cardEffect(village, -1, -1, -1, g, 0, 0);
    handCountAfter = g->handCount[player];
    actionsAfter = g->numActions;
    playedAfter = g->playedCardCount;
  //  printf("Player: %i\n", player);
    // hand count should be the same since 1 card is drawn and the village card is played
    if(handCountBefore != handCountAfter){
    //  printf("Wrong hand count!\n");
      return 0;
    }
    if(actionsAfter != (actionsBefore + 2)){
    //  printf("Wrong action count!\n");
      return 0;
    }
    if(playedAfter != (playedBefore + 1)){
    //  printf("Wrong plsed cards count!\n");
      return 0;
    }
    return 1;
}

int main(int argc, char *argv[]) {
  long seed;
  struct gameState g;
  int players, i, j, good, bad, results;
  int k[10] = {adventurer, gardens, great_hall, village, minion, mine, cutpurse, 
           baron, salvager, smithy};
  bad = 0;
  good = 0;         
  printf("Card Test 1 -> Village Card :\n");
  if(argc < 2){
    printf("Usage: ./unttest3.c [random_seed]\n");
  }
  for (j = 0; j < 1000; j++) {
    for (i = 0; i < sizeof(struct gameState); i++) {
      ((char*)&g)[i] = floor(Random() * 256);
    }
    // give different seed for every test 
    seed = (long)floor(Random()*(long)argv[1]+j);       
    players = j+2;
    // comment out next 2 lines to see bug
    if(players > 4)
      players = players%3+2; // makes sure num players = 2 to 4
    // the game will only work with 2 players as currently written
    initializeGame(2, k, seed, &g);
    results = testVillageCard(2, k, &g);
  if(results == 1){
    //  printf("Test Passed!\n");
      good++;
    }
    else{
    //  printf("Test Failed!\n");
      bad++;
    }
  //  printf("\n");
  }
  printf("Tests passed = %i\n", good);
  printf("Tests Failed = %i\n", bad);
  return 0;
}